#include <click/config.h>
#include <click/confparse.hh>
#include <click/error.hh>
#include "SetTos.hh"

CLICK_DECLS

SetTos::SetTos() {
}

SetTos::~SetTos() {
}

int SetTos::configure(Vector<String> &conf, ErrorHandler *errh) {
	if (cp_va_kparse(conf, this, errh, cpEnd) < 0) return -1;
	
	return 0;
}

Packet* SetTos::Set(Packet *p_in)
{
	if (WritablePacket *p = p_in->uniqueify()) {
		click_ip *ip;
		unsigned plen, hlen;
		
		if (!p->has_network_header())
			goto bad;
		plen = p->network_length();
		if (plen < sizeof(click_ip))
			goto bad;
		ip = p->ip_header();
		ip->ip_tos = 192;
		hlen = ip->ip_hl << 2;
		if (hlen < sizeof(click_ip) || hlen > plen)
			goto bad;
		
		ip->ip_sum = 0;
		ip->ip_sum = click_in_cksum((unsigned char *)ip, hlen);
		return p;
		
	bad:
		click_chatter("SetIPChecksum: bad lengths");
		p->kill();
    }
    return 0;
}

void SetTos::push(int value, Packet *p) {
		
	p = Set(p);
	
	output(0).push(p);
}

CLICK_ENDDECLS
EXPORT_ELEMENT(SetTos)
